/******************************************************************************
 * @brief: gp30 app
 * @Author: aple
 * @Date: 2022-12-14 08:40:58
 * @LastEditors: aple
 * @LastEditTime: 2022-12-14 14:24:05
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "mbapp.h"
#include "os_obj.h" // RTOS object definitions
#include "sys_init.h"
#include "common.h"
#include "bsp_api.h"
#include "dataproc.h"
#include "gui/gui_main.h"
#include "lcd_yl.h"
#include "GP30_task.h"
#include "par/par_tab.h"

/******************************************************************************
 * @brief   arg.
 *****************************************************************************/
extern const unsigned char Font_12[];

/******************************************************************************
 * @brief   micro.
 *****************************************************************************/
#define OPT_DAT_READ 0
#define OPT_TEMP_SET 1
#define OPT_Q_SET 2
#define OPT_Q_GET 3
#define OPT_INFO_SET 4
#define OPT_INFO_GET 5
#define OPT_FLOW_CLR 6
#define OPT_CMD_SP 7

/******************************************************************************
 * @brief   function.
 *****************************************************************************/
void gp30_ui_handle(void)
{
	char buff[128];

	lcd_yl_anti();
	lcd_yl_square_fill(0, 0, 127, 63);
	gui_st.move_site_x = 32;
	gui_st.move_site_y = 16;
	lcd_yl_space_set(0);
	lcd_yl_font_set(16, 16, Font_8_16);
	lcd_yl_normal();

	lcd_yl_square(0, 0, 127, 63);
	lcd_yl_line(0, 18, 127, 18);
	// lcd_yl_line(0,18+16, 127, 18+16);
	// lcd_yl_line(0,18+32, 127, 18+32);

#define DAT_Y_SITE 19
#define DAT_X_SITE 2

	lcd_yl_font_set(6, 12, Font_12);

	switch (gp30_st.ui.opt_type)
	{
	case OPT_DAT_READ:
	{
		static uu32 tick_old;
		
		if ((gp30_st.cmd_req | gp30_st.cmd_rsp) != 0)
			lcd_yl_anti();
		lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: READ DATA", 0);
		lcd_yl_normal();

		// send read cmd per 60s
		if(app_tick_get() - tick_old > 60000)
		{
			gp30_st.cmd_req = GP30_CMD_READ_2301;
			osSignalSet(os_obj.tid_gp30, 0x801);
			tick_old = app_tick_get();
		}
		
		// page 1
		if (gp30_st.ui.page == 1)
		{
			sprintf(buff, "RT = %6.6f m3", (float)gp30_st.read_info.rt_flow_mL / 1000000);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 0, buff, 0);

			sprintf(buff, "ID = %02X%02X%02X%02X%02X",
					gp30_st.read_info.id[0],
					gp30_st.read_info.id[1],
					gp30_st.read_info.id[2],
					gp30_st.read_info.id[3],
					gp30_st.read_info.id[4]);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 14, buff, 0);

			sprintf(buff, "ST1= %02X, ST2=%02X", gp30_st.read_info.st[0], gp30_st.read_info.st[1]);
			lcd_yl_alain_puts(2, DAT_Y_SITE + 28, buff, 0);
		}
		else
		{
			sprintf(buff, "+: %d.%06d m3", (int)gp30_st.read_info.total_flow.m3, (int)gp30_st.read_info.total_flow.mL);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE, buff, 0);

			sprintf(buff, "-: %d.%06d m3", (int)gp30_st.read_info.total_nflow.m3, (int)gp30_st.read_info.total_nflow.mL);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 14, buff, 0);

			sprintf(buff, "T =  %6.2f C", (float)gp30_st.read_info.temp / 100);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 28, buff, 0);

			gp30_st.ui.page = 0;
		}
		break;
	}
	case OPT_TEMP_SET:
	{
		if ((gp30_st.cmd_req | gp30_st.cmd_rsp) != 0)
			lcd_yl_anti();
		lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: SET TEMP", 0);
		lcd_yl_normal();

		sprintf(buff, "SET = %6.2f", (float)sys_par.GP30_temp.ival / 100);
		lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE, buff, 0);
		break;
	}
	case OPT_Q_SET:
	{
		if ((gp30_st.cmd_req | gp30_st.cmd_rsp) != 0)
		{
			lcd_yl_anti();
		}

		if (gp30_st.ui.page == 1)
		{
			// set calibration, abs mode
			gp30_st.q_set[0] = (float)par_get(GP30_Q1) / 1000;
			gp30_st.q_set[1] = (float)par_get(GP30_Q2) / 1000;
			gp30_st.q_set[2] = (float)par_get(GP30_Q3) / 1000;
			gp30_st.q_set_mode = 1;

			lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: SET Q.FIX", 0);
		}
		else if (gp30_st.ui.page == 2)
		{
			// set calibration, flow = 0
			gp30_st.q_set[0] = 99.0f;
			gp30_st.q_set[1] = 99.0f;
			gp30_st.q_set[2] = 0.0f;
			
			gp30_st.q_set_mode = 0;
			lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: SET Q.0L/h", 0);
		}
		else if (gp30_st.ui.page == 3)
		{
			// write 0 to clear calibration value
			gp30_st.q_set[0] = 0.0f;
			gp30_st.q_set[1] = 0.0f;
			gp30_st.q_set[2] = 0.0f;
			gp30_st.q_set_mode = 0;
			lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: CLR Q", 0);
		}
		else
		{
			// set calibration, sum mode
			gp30_st.q_set[0] = (float)par_get(GP30_Q1) / 1000;
			gp30_st.q_set[1] = (float)par_get(GP30_Q2) / 1000;
			gp30_st.q_set[2] = (float)par_get(GP30_Q3) / 1000;
			gp30_st.ui.page = 0;
			gp30_st.q_set_mode = 0;
			lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: SET Q.SUM", 0);
		}

		lcd_yl_normal();

		sprintf(buff, "Q1: %7.3f%%", gp30_st.q_set[0]);
		lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE, buff, 0);

		sprintf(buff, "Q2: %7.3f%%", gp30_st.q_set[1]);
		lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 14, buff, 0);

		sprintf(buff, "Q3: %7.3f%%", gp30_st.q_set[2]);
		lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 28, buff, 0);

		break;
	}
	case OPT_Q_GET:
	{
		if ((gp30_st.cmd_req | gp30_st.cmd_rsp) != 0)
			lcd_yl_anti();
		lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: GET FLOW.Q", 0);
		lcd_yl_normal();

		sprintf(buff, "Q1: %7.3f%%", gp30_st.q_get[0]);
		lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE, buff, 0);

		sprintf(buff, "Q2: %7.3f%%", gp30_st.q_get[1]);
		lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 14, buff, 0);

		sprintf(buff, "Q3: %7.3f%%", gp30_st.q_get[2]);
		lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 28, buff, 0);

		break;
	}
	case OPT_INFO_SET:
	{
		if ((gp30_st.cmd_req | gp30_st.cmd_rsp) != 0)
			lcd_yl_anti();
		lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: SET INFO", 0);
		lcd_yl_normal();
		
		gp30_st.info_set.year = sys_par.GP30_YEAR.uval;
		gp30_st.info_set.mon = sys_par.GP30_MON.uval;
		gp30_st.info_set.dat = sys_par.GP30_DAY.uval;
		gp30_st.info_set.hour = sys_par.GP30_HOUR.uval;
		gp30_st.info_set.min = sys_par.GP30_MIN.uval;
		gp30_st.info_set.sec = sys_par.GP30_SEC.uval;
		
		gp30_st.info_set.dev_id =  par_get(GP30_DEV_ID);
		gp30_st.info_set.fac_code = par_get(GP30_FAC_ID);
		
		if(sc_pulse.wave_1s)
		sprintf(buff, "DAT: %04d-%02d-%02d", gp30_st.info_set.year, gp30_st.info_set.mon, gp30_st.info_set.dat);
		else
			sprintf(buff, "TIM: %02d:%02d:%02d", gp30_st.info_set.hour, gp30_st.info_set.min, gp30_st.info_set.sec);
	
		lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE, buff, 0);

		sprintf(buff, "FAC: %06d", (unsigned int)gp30_st.info_set.fac_code);
		lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 14, buff, 0);
		sprintf(buff, "DEV: %08d", (unsigned int)gp30_st.info_set.dev_id);
		lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 28, buff, 0);
		
		
		
		break;
	}
	case OPT_INFO_GET:
	{
		if ((gp30_st.cmd_req | gp30_st.cmd_rsp) != 0)
			lcd_yl_anti();
		lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: GET INFO", 0);
		lcd_yl_normal();
		
		if(gp30_st.ui.page == 2)
		{
			sprintf(buff, "TIM-YEAR: %d", gp30_st.meter_rd.rtct.year);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE, buff, 0);
			
			sprintf(buff, "TIM-MON : %d", gp30_st.meter_rd.rtct.mon);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 14, buff, 0);
			
			sprintf(buff, "TIM-DAY : %d", gp30_st.meter_rd.rtct.day);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 28, buff, 0);
		}
		else if(gp30_st.ui.page == 1)
		{
			sprintf(buff, "PIPE: %d", gp30_st.meter_rd.pipe_size);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE, buff, 0);

			sprintf(buff, "TYPE : %02X", (unsigned int)gp30_st.meter_rd.dev_type_bcd);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 14, buff, 0);

			sprintf(buff, "VERSION: %02X", (unsigned int)gp30_st.meter_rd.version_bcd);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 28, buff, 0);
		}
		else
		{
			sprintf(buff, "FAC CODE: %04X", gp30_st.meter_rd.fac_code_bcd);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE, buff, 0);

			sprintf(buff, "SN : %08X", (unsigned int)gp30_st.meter_rd.serial_bcd);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 14, buff, 0);

			sprintf(buff, "DID: %08X", (unsigned int)gp30_st.meter_rd.dev_id_bcd);
			lcd_yl_alain_puts(DAT_X_SITE, DAT_Y_SITE + 28, buff, 0);

			gp30_st.ui.page = 0;			
		}

		break;
	}
	case OPT_FLOW_CLR:
	{
		if ((gp30_st.cmd_req | gp30_st.cmd_rsp) != 0)
			lcd_yl_anti();
		lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: CLR FLOW", 0);
		lcd_yl_normal();
				
		break;
	}
	default:
	{
		if ((gp30_st.cmd_req | gp30_st.cmd_rsp) != 0)
			lcd_yl_anti();
		lcd_yl_alain_puts(DAT_X_SITE, 2, "CMD: OPT NONE", 0);
		lcd_yl_normal();
		break;
	}
	}

	lcd_yl_space_set(1);
}
FUN_LIST_EXPORT(GUI, gp30_ui_handle);

/*******************************************************************************
 * @brief  key s1 handle :
 * \param[in] none
 * \retval: none
 *******************************************************************************/
void gp30_key_s1(void)
{
	var_limit_inc(gp30_st.ui.opt_type, OPT_CMD_SP);
	gp30_st.ui.page = 0;
}
FUN_LIST_EXPORT(KEY_S1, gp30_key_s1);

/*******************************************************************************
 * @brief  key s2 handle :
 * \param[in] none
 * \retval: none
 *******************************************************************************/
void gp30_key_s2(void)
{
	var_limit_dec(gp30_st.ui.opt_type, 0);
	gp30_st.ui.page = 0;
}
FUN_LIST_EXPORT(KEY_S2, gp30_key_s2);

/*******************************************************************************
 * @brief  key s3 handle :
 * \param[in] none
 * \retval: none
 *******************************************************************************/
void gp30_key_s3(void)
{
	gp30_st.ui.page++;
}
FUN_LIST_EXPORT(KEY_S3, gp30_key_s3);

/*******************************************************************************
 * @brief  key s4 handle :
 * \param[in] none
 * \retval: none
 *******************************************************************************/
void gp30_key_s4(void)
{
	switch (gp30_st.ui.opt_type)
	{
	case OPT_DAT_READ:
		gp30_st.cmd_req = GP30_CMD_READ_2301;
		osSignalSet(os_obj.tid_gp30, 0x801);
		break;
	case OPT_TEMP_SET:
		gp30_st.cmd_req = GP30_CMD_T_SET;
		osSignalSet(os_obj.tid_gp30, 0x801);
		break;
	case OPT_Q_SET:
		gp30_st.cmd_req = GP30_CMD_Q_SET;
		osSignalSet(os_obj.tid_gp30, 0x801);
		break;
	case OPT_Q_GET:
		gp30_st.cmd_req = GP30_CMD_METER_RD;
		osSignalSet(os_obj.tid_gp30, 0x801);
		break;
	case OPT_INFO_SET:
		gp30_st.cmd_req = GP30_CMD_INFO_SET;
		osSignalSet(os_obj.tid_gp30, 0x801);
		break;
	case OPT_INFO_GET:
		gp30_st.cmd_req = GP30_CMD_INFO_GET;
		osSignalSet(os_obj.tid_gp30, 0x801);
		break;
	case OPT_FLOW_CLR:
		gp30_st.cmd_req = GP30_CMD_CLR_FLOW;
		osSignalSet(os_obj.tid_gp30, 0x801);
		break;
	default:
		break;
	}
}
FUN_LIST_EXPORT(KEY_S4, gp30_key_s4);
